package cn.edu.sgu.www.mhxysy.controller.waiguan;

import cn.edu.sgu.www.mhxysy.consts.CustomConstants;
import cn.edu.sgu.www.mhxysy.consts.CustomMessages;
import cn.edu.sgu.www.mhxysy.consts.MimeTypes;
import cn.edu.sgu.www.mhxysy.dto.waiguan.RoleShizhuangImageDTO;
import cn.edu.sgu.www.mhxysy.entity.waiguan.RoleShizhuangImage;
import cn.edu.sgu.www.mhxysy.pager.waiguan.RoleShizhuangImagePager;
import cn.edu.sgu.www.mhxysy.restful.JsonPage;
import cn.edu.sgu.www.mhxysy.restful.JsonResult;
import cn.edu.sgu.www.mhxysy.service.waiguan.RoleShizhuangImageService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.github.xiaoymin.knife4j.annotations.ApiSupport;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import java.io.IOException;

/**
 * @author 沐雨橙风ιε
 * @version 1.0
 */
@Api(tags = "角色时装图片管理")
@ApiSupport(author = CustomConstants.AUTHOR)
@Validated
@RestController
@RequestMapping(path = "/role_shizhuang_image", produces = MimeTypes.APPLICATION_JSON_CHARSET_UTF_8)
public class RoleShizhuangImageController {

    private final RoleShizhuangImageService roleShizhuangImageService;

    @Autowired
    public RoleShizhuangImageController(RoleShizhuangImageService roleShizhuangImageService) {
        this.roleShizhuangImageService = roleShizhuangImageService;
    }

    @ApiOperation("上传图片")
    @RequestMapping(value = "/upload", method = RequestMethod.POST)
    public JsonResult<String> upload(MultipartFile file) throws IOException {
        String url = roleShizhuangImageService.upload(file);

        return JsonResult.success(CustomMessages.UPLOAD_SUCCESS, url);
    }

    @ApiOperation("添加角色时装图片")
    @ApiOperationSupport(ignoreParameters = "id")
    @RequestMapping(value = "/insert", method = RequestMethod.POST)
    public JsonResult<Void> insert(@Validated RoleShizhuangImageDTO roleShizhuangImageDTO) {
        roleShizhuangImageService.insert(roleShizhuangImageDTO);

        return JsonResult.success(CustomMessages.INSERT_SUCCESS);
    }

    @ApiOperation("通过ID删除角色时装图片")
    @RequestMapping(value = "/deleteById", method = RequestMethod.POST)
    public JsonResult<Void> deleteById(
            @ApiParam(name = "id", value = "时装图片ID", required = true)
            @NotNull(message = CustomMessages.ID_NOT_NULL) Integer id) {
        roleShizhuangImageService.deleteById(id);

        return JsonResult.success(CustomMessages.DELETE_SUCCESS);
    }

    @ApiOperation("通过ID修改角色时装图片信息")
    @RequestMapping(value = "/updateById", method = RequestMethod.POST)
    public JsonResult<Void> updateById(@Validated RoleShizhuangImageDTO roleShizhuangImageDTO) {
        roleShizhuangImageService.updateById(roleShizhuangImageDTO);

        return JsonResult.success(CustomMessages.UPDATE_SUCCESS);
    }

    @ApiOperation("通过ID修改角色时装图片")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "角色时装图片ID", required = true),
            @ApiImplicitParam(name = "image", value = "图片地址", required = true)
    })
    @RequestMapping(value = "/updateImageById", method = RequestMethod.POST)
    public JsonResult<Void> updateImageById(
            @NotNull(message = CustomMessages.ID_NOT_NULL) Integer id,
            @NotBlank(message = CustomMessages.IMAGE_NOT_NULL) String image) {
        roleShizhuangImageService.updateImageById(id, image);

        return JsonResult.success(CustomMessages.UPDATE_SUCCESS);
    }

    @ApiOperation("分页查询角色时装图片列表")
    @RequestMapping(value = "/selectByPage", method = RequestMethod.POST)
    public JsonResult<JsonPage<RoleShizhuangImage>> selectByPage(RoleShizhuangImagePager pager) {
        Page<RoleShizhuangImage> page = roleShizhuangImageService.selectByPage(pager);

        return JsonResult.restPage(page);
    }

}